package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.BusinessIndividual;
import com.innovations.inn.domain.response.ResponseBusinessIndividualInfo;
import com.innovations.inn.service.IBusinessIndividualService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商业用户信息Controller
 *
 * @author dp
 * @date 2024-08-17
 */
@Api(tags = "商业用户信息")
@RestController
@RequestMapping("/inn/individual")
public class BusinessIndividualController extends BaseController {
    @Autowired
    private IBusinessIndividualService businessIndividualService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询商业用户信息列表
     */
    @ApiOperation("查询商业用户信息列表")
    @PreAuthorize("@ss.hasPermi('inn:individual:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusinessIndividual businessIndividual) {
        startPage();
        List<BusinessIndividual> list = businessIndividualService.selectBusinessIndividualList(businessIndividual);
        return getDataTable(list);
    }

    /**
     * 查询商业用户详细信息列表
     */
    @ApiOperation("查询商业用户详细信息列表")
    @PreAuthorize("@ss.hasPermi('inn:individual:list')")
    @GetMapping("/list/info")
    public TableDataInfo listInfo(BusinessIndividual businessIndividual) {
        startPage();
        List<ResponseBusinessIndividualInfo> list = businessIndividualService.selectBusinessIndividualListInfo(businessIndividual);
        return getDataTable(list);
    }

    /**
     * 导出商业用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('inn:individual:export')")
    @Log(title = "商业用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusinessIndividual businessIndividual) {
        List<BusinessIndividual> list = businessIndividualService.selectBusinessIndividualList(businessIndividual);
        ExcelUtil<BusinessIndividual> util = new ExcelUtil<BusinessIndividual>(BusinessIndividual.class);
        util.exportExcel(response, list, "商业用户信息数据");
    }

    /**
     * 获取商业用户信息详细信息
     */
    @ApiOperation("获取商业用户信息详细信息")
    @PreAuthorize("@ss.hasPermi('inn:individual:query')")
    @GetMapping(value = "/{businessIndividualId}")
    public AjaxResult getInfo(@PathVariable("businessIndividualId") Long businessIndividualId) {
        return success(businessIndividualService.selectBusinessIndividualByBusinessIndividualId(businessIndividualId));
    }

    /**
     * 新增商业用户信息
     */
    @ApiOperation("新增商业用户信息")
    @PreAuthorize("@ss.hasPermi('inn:individual:add')")
    @Log(title = "商业用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody BusinessIndividual businessIndividual) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        businessIndividualService.completeData(businessIndividual, userId, 1);
        return toAjax(businessIndividualService.insertBusinessIndividual(businessIndividual));
    }

    /**
     * 修改商业用户信息
     */
    @ApiOperation("修改商业用户信息")
    @PreAuthorize("@ss.hasPermi('inn:individual:edit')")
    @Log(title = "商业用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody BusinessIndividual businessIndividual) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        businessIndividualService.completeData(businessIndividual, userId, 2);
        return toAjax(businessIndividualService.updateBusinessIndividual(businessIndividual));
    }

    /**
     * 删除商业用户信息
     */
    @ApiOperation("删除商业用户信息")
    @PreAuthorize("@ss.hasPermi('inn:individual:remove')")
    @Log(title = "商业用户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{businessIndividualIds}")
    public AjaxResult remove(@PathVariable Long[] businessIndividualIds) {
        return toAjax(businessIndividualService.deleteBusinessIndividualByBusinessIndividualIds(businessIndividualIds));
    }
}
